﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 水果计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int num = int.Parse(txtMoney.Text);
            int A = int.Parse(txt1.Text);
            int B = int.Parse(txt2.Text);
            int C = int.Parse(txt3.Text);
            int D = int.Parse(txt4.Text);

            int[] x = new int[] {A, B, C, D};
            int[] n = new int[4];
            int count = 0;
            search(num, x, n, 0, ref count, ref listBox1);
            listBox1.Items.Add($"一共有{count}种方案");
        }

        public static void search(int money, int[] x, int[] n, int index, ref int count,ref ListBox listBox)
        {
            int num = money / x[index];
            if (num == 0)
            {
                // 
                if (money >= 0 && money < x[0])
                {
                    listBox.Items.Add($"苹果：{n[0]} ,西瓜：{n[1]} ,车厘子：{n[2]} ,榴莲：{n[3]}");
                    count++;
                }
                return;
            }
            else if (num > 0)
            {
                n[index]++;
                // 继续减去自己
                search(money - x[index], x, n, index, ref count,ref listBox);
                // 数量回退
                n[index]--;
                if (index < 3)
                {
                    //n[index]--;
                    search(money, x, n, index + 1, ref count,ref listBox);
                }
            }
            return;
        }
    }
}
